package view.rules;

import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import managers.FeatureManager;
import features.DBFeatureValues;
import features.Feature;

public class SelectFeatStructView extends JFrame{

	private JButton btnOK;
	private JButton btnCancel;
	private JLabel lblFeature;
	private JTable featureTable ;
	private JScrollPane scrollPane;
	
	DefaultTableModel featureModel;
	
	public SelectFeatStructView(){
		setTitle("Features");
	    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	    setVisible(true);
	    getContentPane().setLayout(null);
	    
	    scrollPane = new JScrollPane();
	    scrollPane.setBounds(51, 90, 975, 431);
	    getContentPane().add(scrollPane);
	    
	    btnOK = new JButton("OK");
	    btnOK.setBounds(796, 541, 89, 23);
	    getContentPane().add(btnOK);
	    
	    btnCancel = new JButton("Cancel");
	    btnCancel.setBounds(937, 541, 89, 23);
	    getContentPane().add(btnCancel);
	    setVisible(false);
	    
	    setBounds(100, 50, 1100, 650);
	    
	}
	
	public void createTable(String pos){
		featureModel = new DefaultTableModel();
		
		 ArrayList<Feature> featureList = FeatureManager.getDefaultFeatures(pos);
		for(Feature feature : featureList)
		{
			DBFeatureValues valueList = FeatureManager.getFeatureValues(pos, feature.getName());
			Object[] values = valueList.getValues().toArray();

			ArrayList<Boolean> bool = new ArrayList<Boolean>();
			for(Object value: values)
				bool.add(Boolean.FALSE);
			
			featureModel.addColumn("", bool.toArray());
			featureModel.addColumn(feature.getName(), values);
		}
		
		featureTable = new JTable(featureModel)
		  {
		        private static final long serialVersionUID = 1L;
		        public boolean isCellEditable(int row, int column){
		        	if(column%2 == 0)
		        		return true;
		        	else
		        		return false;
		        }
		        @Override
		        public Class getColumnClass(int column) {
		            switch (column%2) {
		                case 0:
		                    return Boolean.class;
		                default:
		                    return String.class;
		            }
		        }
		    };

		    for(int i = 0; i < featureModel.getColumnCount(); i++)
		    {
		    	TableColumn column = featureTable.getColumnModel().getColumn(i);
		    	switch(i%2)
		    	{
		    		case 0:	column.setPreferredWidth(25);break;
		    		case 1: column.setPreferredWidth(150);break;
		    	} 
		    }
		    
		    scrollPane.setViewportView(featureTable);
	}
	
	public JButton getBtnOK() {
		return btnOK;
	}

	public JButton getBtnCancel() {
		return btnCancel;
	}

	public JTable getFeatureTable() {
		return featureTable;
	}

	
}
